﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DeletePosition]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_DeletePosition];
GO
CREATE PROCEDURE [dbo].[sproc_DeletePosition]
    @Position_id int
/*

============================================================
功能:    删除职位
参数:
    @Position_id int    ：    被删除职位的ID
============================================================

*/
AS
IF @Position_id>0
BEGIN
    --有下级职位
    IF EXISTS(SELECT 1 FROM uds_Position WHERE super_Position_id =@Position_id)
        RETURN -1
    --有职位成员(在岗的）
    IF EXISTS(SELECT 1 FROM uds_staff a,uds_staff_in_position b WHERE b.Position_id =@Position_id and a.dimission = 0 and a.staff_id = b.staff_id)
        RETURN -2
    --删除职位人员
    DELETE FROM uds_staff_in_position
        WHERE Position_id =@Position_id 
    --删除流程绑定职位
    DELETE FROM uds_flow_member_bind
        WHERE obj_id = @Position_id AND obj_type = 1
    --删除职位权限
    DELETE FROM UDS_Assign_Rule 
        WHERE Position_id = @Position_id
    --删除职位接点
    DELETE FROM uds_Position 
        WHERE Position_id = @Position_id
    RETURN 0
END
ELSE
    RETURN -1